/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validation;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.Resources;

/**
 *
 * @author Antonio
 */
public class StrutsValidator {

    public static boolean validateTwoFields(
            Object bean,
            ValidatorAction va,
            Field field,
            ActionMessages errors,
            Validator validator,
            HttpServletRequest request) {

        String value = ValidatorUtils.getValueAsString(
                bean,
                field.getProperty());
        String sProperty2 = field.getVarValue("secondProperty");
        String value2 = request.getParameter(sProperty2);


        if (!GenericValidator.isBlankOrNull(value)) {
            try {
                if (!value.equals(value2)) {
                    errors.add(field.getKey(),
                            Resources.getActionMessage(
                            request,
                            va,
                            field));
                    return false;
                }
            } catch (Exception e) {
                errors.add(field.getKey(),
                        Resources.getActionMessage(
                        request,
                        va,
                        field));
                return false;
            }
        }

        return true;
    }

    public static boolean validateFecha(
            Object bean,
            ValidatorAction va,
            Field field,
            ActionMessages errors,
            Validator validator,
            HttpServletRequest request) {

        String anyo = ValidatorUtils.getValueAsString(
                bean,
                field.getProperty());

        String sProperty2 = field.getVarValue("diaProperty");
        String dia = ValidatorUtils.getValueAsString(
                bean,
                sProperty2);


        String sProperty3 = field.getVarValue("mesProperty");
        String mes = ValidatorUtils.getValueAsString(
                bean,
                sProperty3);

        if (!GenericValidator.isBlankOrNull(anyo)) {
            try {
                if (!correctaFecha(dia, mes, anyo)) {
                    errors.add(field.getKey(),
                            Resources.getActionMessage(
                            request,
                            va,
                            field));
                    return false;
                }
            } catch (Exception e) {
                errors.add(field.getKey(),
                        Resources.getActionMessage(
                        request,
                        va,
                        field));
                return false;
            }
        }

        return true;
    }

    private static boolean bisiesto(String anyo) {

        Integer anyoEntero = new Integer(anyo);
        if ((anyoEntero % 4 == 0 && anyoEntero % 100 != 0) || anyoEntero % 400 == 0) {
            return true;
        }
        return false;
    }

    private static boolean correctaFecha(String dia, String mes, String anyo) {

        Integer diaEntero = new Integer(dia);
        if (mes.equals("4") || mes.equals("6") || mes.equals("9") || mes.equals("11")) {
            if (dia.equals("31")) {
                return false;
            }
        } else if (mes.equals("2")) {
            if (bisiesto(anyo)) {
                if (diaEntero > 29) {
                    return false;
                }
            } else {
                if (diaEntero > 28) {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean validateNombreCompleto(
            Object bean,
            ValidatorAction va,
            Field field,
            ActionMessages errors,
            Validator validator,
            HttpServletRequest request) {
        String nombre = ValidatorUtils.getValueAsString(
                bean,
                field.getProperty());
        String sProperty2 = field.getVarValue("apellido");
        String apellidos = ValidatorUtils.getValueAsString(
                bean,
                sProperty2);

         try {
                if ((nombre.equals("") && (!apellidos.equals("")))||
                        (apellidos.equals("") && (!nombre.equals("")))) {
                    errors.add(field.getKey(),
                            Resources.getActionMessage(
                            request,
                            va,
                            field));
                    return false;
                }
            } catch (Exception e) {
                errors.add(field.getKey(),
                        Resources.getActionMessage(
                        request,
                        va,
                        field));
                return false;
            }
        
         return true;
    }
}
